package com.shopping.shoppingdemo.service.impl;

import cn.idev.excel.FastExcel;
import com.shopping.shoppingdemo.domain.dto.StatisticsDayDTO;
import com.shopping.shoppingdemo.domain.dto.StatisticsDayExcelDO;
import com.shopping.shoppingdemo.service.FileService;
import com.shopping.shoppingdemo.service.RequestCountService;
import com.shopping.shoppingdemo.utils.converter.StatisticsConverter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author Yandel
 * @date 2025/10/20 下午8:38
 * @description:
 */
@Slf4j
@Service
public class FileServiceImpl implements FileService {

    @Autowired
    private RequestCountService requestCountService;




    @Override
    public void download2Excel(Integer day, HttpServletResponse response) {
        List<StatisticsDayDTO> pvDayList = requestCountService.getPvUvDayList(day);
        // StatisticsDayDTO 转 StatisticsDayExcelDTO
        List<StatisticsDayExcelDO> excelDTOList = StatisticsConverter.convertToExcelDOList(pvDayList);

        // 使用 FastExcel 导出 Excel
        // TODO 这里可以用一个大文件，比如说 10万条数据测试一下，看看 FastExcel 的性能
        try {
            FastExcel.write(response.getOutputStream(), StatisticsDayExcelDO.class)
                    .sheet(day + "天统计")
                    .doWrite(excelDTOList);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
